package com.teach.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.PreparedStatement;


public class JdbcUtil {

	private static String url;
	private static String driverName;
	private static String username;
	private static String password;

	static {
		Properties pro = new Properties();
		try {
			pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("sql.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		driverName = pro.getProperty("driverName");
		url = pro.getProperty("url");
		username = pro.getProperty("username");
		password = pro.getProperty("password");
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void close(Connection conn, PreparedStatement stmt) {
		try {
			stmt.close();
		} catch (Exception ex) {
		}
		try {
			conn.close();
		} catch (Exception ex) {
		}
	}
	
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}
	
	public static void close(Connection conn, PreparedStatement stmt,ResultSet rs) {
		try {
			stmt.close();
		} catch (Exception ex) {
		}
		try {
			conn.close();
		} catch (Exception ex) {
		}
	}
	
	
}
